package md.lanjing.com.moduledev;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lxh.baselibray.base.XActivity;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import md.lanjing.com.module_deal_center.ui.fragment.DealCenterFragment;
import md.lanjing.com.module_home.ui.adapter.MainFragmentPagerAdapter;
import md.lanjing.com.module_home.ui.fragment.HomeFragment;
import md.lanjing.com.module_mine.ui.fragment.PersonCenterFragment;
import md.lanjing.com.module_wallet.ui.fragment.MyWalletFragment;

@Route(path = ARouterConfig.MAIN_act)
public class MainActivity extends XActivity implements RadioGroup.OnCheckedChangeListener{

    @BindView(R.id.main_viewPager)
    ViewPager viewPager;
    @BindView(R.id.main_rg)
    RadioGroup rg;

    @Override
    public void initData(Bundle savedInstanceState) {
     //   viewPager=findViewById(R.id.main_viewPager);
     //   rg=findViewById(R.id.main_rg);
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new MyWalletFragment());
        fragmentList.add(new DealCenterFragment());
        fragmentList.add(new PersonCenterFragment());

        MainFragmentPagerAdapter adapter = new MainFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        rg.check(R.id.rb_home);
                        break;
                    case 1:
                        rg.check(R.id.rb_my_wallet);
                        break;
                    case 2:
                        rg.check(R.id.rb_deal_center);
                        break;
                    case 3:
                        rg.check(R.id.rb_person_center);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        rg.setOnCheckedChangeListener(this);
        rg.getChildAt(0).performClick();

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_home) {
            viewPager.setCurrentItem(0);

        } else if (checkedId == R.id.rb_my_wallet) {
            viewPager.setCurrentItem(1);

        } else if (checkedId == R.id.rb_deal_center) {
            viewPager.setCurrentItem(2);

        } else if (checkedId == R.id.rb_person_center) {
            viewPager.setCurrentItem(3);

        }
    }
    private long timeMillis;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - timeMillis) > 2000) {
                ToastUtils.showLongToast(context,"再按一次退出");
                timeMillis = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
